﻿
using System ;

using MskEdit.Sdk ;


namespace MskEdit.Dxf.Model
{
    public class DxfSection
    {
        private string[]    allLines_ ;
        private SdkSubrange subrange_ ;


        private DxfSection( string[] allLines, SdkSubrange subrange )
        {
            allLines_ = allLines ;
            subrange_ = subrange ;
        }

        public static DxfSection TryFrom( string[] allLines, ref int lineNo, DxfSection fallbackValue ) {
        
            int internalLineNo = lineNo + 3 ;
            
            if ( allLines.Length <= (internalLineNo) )
                return fallbackValue ;
                
            if ( ! "0".Equals( allLines[internalLineNo-3] ) )
                return fallbackValue ;
                
            if ( ! "SECTION".Equals( allLines[internalLineNo-2] ) )
                return fallbackValue ;
                  
            bool sectionFound = false ;
            
            for ( ; internalLineNo < allLines.Length; ++internalLineNo ) {
            
                sectionFound = "ENDSEC".Equals( allLines[internalLineNo] ) ;
                
                if ( sectionFound ) {
                    internalLineNo += 1 ;
                    break ;
                }
            }
            
            if ( ! sectionFound )
                return fallbackValue ;
                
            SdkSubrange sectionSubrange = new SdkSubrange( lineNo, internalLineNo - lineNo ) ;
            
            lineNo = internalLineNo ;
            
            return new DxfSection( allLines, sectionSubrange ) ;
        }
        
        
        private SdkSubrange InnerSubrange {
            get { return new SdkSubrange( subrange_.Begin + 2, subrange_.Length - 4 ) ; }
        }


        public DxfHeader TryGetHeader( DxfHeader fallbackValue ) {
        
            if ( ! "HEADER".Equals( allLines_[subrange_.Begin+3] ) )
                return fallbackValue ;
                            
            return new DxfHeader( allLines_, this.InnerSubrange ) ;
        }


        public DxfClasses TryGetClasses( DxfClasses fallbackValue ) {
        
            if ( ! "CLASSES".Equals( allLines_[subrange_.Begin+3] ) )
                return fallbackValue ;
                            
            return new DxfClasses( allLines_, this.InnerSubrange ) ;
        }


        public DxfTables TryGetTables( DxfTables fallbackValue ) {
        
            if ( ! "TABLES".Equals( allLines_[subrange_.Begin+3] ) )
                return fallbackValue ;
                            
            return new DxfTables( allLines_, this.InnerSubrange ) ;
        }


        public DxfBlocks TryGetBlocks( DxfBlocks fallbackValue ) {
        
            if ( ! "BLOCKS".Equals( allLines_[subrange_.Begin+3] ) )
                return fallbackValue ;
                            
            return new DxfBlocks( allLines_, this.InnerSubrange ) ;
        }


        public DxfEntities TryGetEntities( DxfEntities fallbackValue ) {
        
            if ( ! "ENTITIES".Equals( allLines_[subrange_.Begin+3] ) )
                return fallbackValue ;
                            
            return new DxfEntities( allLines_, this.InnerSubrange ) ;
        }


        public DxfObjects TryGetObjects( DxfObjects fallbackValue ) {
        
            if ( ! "OBJECTS".Equals( allLines_[subrange_.Begin+3] ) )
                return fallbackValue ;
                            
            return new DxfObjects( allLines_, this.InnerSubrange ) ;
        }

        public string EntityType {
            get { return allLines_[ subrange_.Begin + 3 ] ; }
        }

/*
        public DxfHeader Classes {
            get { return dxfClasses_ ; }
        }


        public DxfClasses Classes {
            get { return dxfClasses_ ; }
        }


        public DxfTables Tables {
            get { return dxfTables_ ; }
        }
*/   
    }
}
